<?php

 /*text field*/
 
/*class:*/

class Admin_NewsController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	parent::init();
    }
    
    public function indexAction()
    {
    	
    	$this->_forward('list','news','admin');
    }
    
    public function createNewsAction()
    {
    	$form = new Admin_Form_CreateNews();
    	$form->setMethod('post');
    	$form->setAction('');
    	if ($this->getRequest()->isPost())
    	{
    		if ($form->isValid($_POST))
    		{
    			$newsmodel = new Admin_Model_News();
    			$headline = $form->getValue('title'); 
    			$category_id = $form->getValue('category');
    			$description = $form->getValue('summary');
    			$content = $form->getValue('body');
    			$newsmodel->addNews($headline, $category_id, $description, $content);
    			$this->_forward('list','news','admin');
    		}
    	}
    	
    	$this->view->form = $form;
    }
    
    public function createCategoryAction()
    {
   	 	$form = new Admin_Form_CreateNews();
    	$form->setMethod('post');
    	$form->setAction('');
    	if ($this->getRequest()->isPost())
    	{
    		if ($form->isValid($_POST))
    		{
    			$model = new Admin_Model_Category();
    			$name = $form->getValue('categoryname');
    			$description = $form->getValue('description');
    			$order = $form->getValue('order');

    			$model->addCategory($name,$description,$order);
    			
    			$this->_forward('inform');
    		}
    	}
    	
    	$this->view->form = $form;
    }
    
    public function informAction()
    {
    	
    }
    
    public function listAction()
    {
    	$model = new Admin_Model_News();
    	$adapter = $model->fetchPaginatorAdapter();
    	
    	$paginator = new Zend_Paginator($adapter);
    	$paginator->setItemCountPerPage(10);
    	$page = $this->getRequest()->getParam('Page',1);
    	$paginator->setCurrentPageNumber($page);
    	$this->view->paginator = $paginator;
    }
    
    public function editAction()
    {
    	$model = new Admin_Model_Category();
    	$categories = $model->getCategoryList()->toArray();
    	$form = new Admin_Form_CreateNews();
    	
    	/*$hiddenid = new Zend_Form_Element_Hidden('news_id');
    	
    	$categoryList = new Zend_Form_Element_Select('categories_id');
    	$categoryList->setLabel('Category:');
    	foreach ($categories as $id=>$category)
    		$categoryList->addMultiOption($id,$category['categories_name']);
    		
    	$HeadLine = new Zend_Form_Element_Text('news_subject');
		$HeadLine->setLabel('HeadLine:')
				->setRequired(true);
				
		$image = new Zend_Form_Element_File('image');
		$image->setLabel('Select an Image');
		
		$Description = new Zend_Form_Element_Text('news_shortcontent');
		$Description->setLabel('Description:')
					->setRequired(true);
					
		$Content = new Zend_Form_Element_Textarea('news_content');
		$Content->setLabel('Content:')
				->setRequired('true');
				
		$submit = new Zend_Form_Element_Submit('post');
		$submit->setLabel('Update');
    			
    	$form->addElements(array($hiddenid,$HeadLine,$categoryList,$image,$Description,$Content,$submit));
    	*/
    	$form->setMethod('post');
    	$form->setAction('');
    	
    	if ($this->getRequest()->isPost())
    	{
    		if ($form->isValid($_POST))
    		{
    			$newsmdl = new Admin_Model_news();
    			$newsmdl->updateNews(
    									$form->getValue('contentId'),
    									$form->getValue('title'),
    									$form->getValue('categoryId'),
    									$form->getValue('summary'),
    									$form->getValue('body')
    								);
    			$this->_forward('List','News','Admin');
    		}
    	}
    	
    	$newsmodel = new Admin_Model_news();
    	$newsid = $this->getRequest()->getParam('id');
    	$this->view->id = $newsid;
    	$news = $newsmodel->find($newsid)->current();
    	$form->populate($news->toArray());
    	$this->view->news = $news;
    	$this->view->form = $form;
    }
    
    public function deleteAction()
    {
    	$id = $this->getRequest()->getParam('id');
    	$model = new Admin_Model_news();
    	$model->deleteNews($id);
    	$this->_forward('list','news','admin');
    }
}